from flask import Blueprint, request
import re
from models.model import *
from models import model
from db_init import db

kind = Blueprint('/kinds', __name__, url_prefix="/kinds")


# 获取注册的用户
@kind.route('/user', methods=['Get'])
def user():
    users = User.query.all()
    user_lst = [{
        'id': i.id,
        'name': i.name,
        'phone': i.phone,
        'img': i.img,
        'money': i.money,
    } for i in users]
    return {'msg': 'ok', 'data': user_lst}


# 添加类别
@kind.route("/cate", methods=["POST"])
def cate():
    cname = request.form.get('cname')
    if not cname:
        return {'msg': '字段不完整', 'code': 400}
    if Cate.query.filter_by(name=cname).all():
        return {'msg': "类别已存在", 'code': 400}
    cate_lst = Cate(name=cname)
    db.session.add(cate_lst)
    db.session.commit()
    return {'msg': "添加类别", 'code': 201}


# 添加商品
@kind.route('/goods', methods=['POST'])
def goods():
    cate_id = request.args.get('cid')
    gname = request.form.get('gname')
    price = request.form.get('price')
    num = request.form.get('num')
    img = request.form.get('img')
    content = request.form.get('content')
    if not all([gname, price, num, img, content]):
        return {'msg': '字段不完整', 'code': 400}
    if not Cate.query.filter_by(id=cate_id).all():
        return {"msg": "这个类别不存在", 'code': 400}
    if Goods.query.filter_by(name=gname).all():
        return {'msg': "商品已存在", 'code': 400}
    good_lst = Goods(name=gname, price=price, num=num, img=img, content=content, cate_id=cate_id)
    db.session.add(good_lst)
    db.session.commit()
    return {'msg': '添加商品成功', 'code': 201}


@kind.route('/<url_data>', methods=['put'])
def update_good(url_data):
    table = url_data.title()
    good = request.json
    g_id = good.pop('id')
    if hasattr(model, table):
        getattr(model, table).query.filter_by(id=g_id).update(good)
    else:
        return {'msg': 'url不对', 'code': 404, 'data': good}
    db.session.commit()
    return {'msg': '修改成功', 'code': 200, 'data': good}


# 添加商户
@kind.route('/business', methods=['POST'])
def business():
    bname = request.form.get('bname')
    addr = request.form.get('addr')
    opend = request.form.get('open')
    tel = request.form.get('tel')
    if not all([bname, addr, opend, tel]):
        return {'msg': '字段不完整', 'code': 400}
    if not re.match(r'^1[3-9]\d{9}$', tel):
        return {'msg': '手机号不符合格式', 'code': 400}
    business_lst = Business(name=bname, addr=addr, open=opend, tel=tel)
    db.session.add(business_lst)
    db.session.commit()
    return {'msg': '添加商户成功', 'code': 201}


# 添加小料
@kind.route('/small', methods=['POST'])
def small():
    name = request.form.get('name')
    price = request.form.get('price')
    if not all([name, price]):
        return {'msg': "字段不完整", 'code': 400}
    if Small.query.filter_by(name=name).all():
        return {'msg': '小料已存在', 'code': 400}
    small_lst = Small(name=name, price=price)
    db.session.add(small_lst)
    db.session.commit()
    return {'msg': "小料添加成功", 'code': 201}
